<div class="layui-fluid layui-anim febs-anim" id="febs-breakageStat" lay-title="损坏统计">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="breakageStat-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">图书名称</label>
                    <div class="layui-input-inline">
                      <input type="text" name="bookname" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">书籍类型</label>
                    <div class="layui-input-inline">
                      <input type="text" name="booktype" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">报损类型</label>
                    <div class="layui-input-inline">
                      <select name="bookfrom">
                        <option value=""></option>
                        <option value="1">正常报损</option>
                        <option value="2">退回报损</option>
                        <option value="3">意外报损</option>
                      </select>
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="breakageStatTable" lay-data="{id: 'breakageStatTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="breakageStat-bookfrom">
  {{#
  var bookfrom = {
  3: {title: '意外损坏', color: 'red'},
  2: {title: '退书损坏', color: 'blue'},
  1: {title: '正常损坏', color: 'green'},
  }[d.bookfrom];
  }}
  <span class="layui-badge febs-tag-{{bookfrom.color}}">{{ bookfrom.title }}</span>
</script>
<script type="text/html" id="breakageStat-omit">
  {{#
  var omit = {
  1: {title: '已审批', color: 'green'},
  0: {title: '未审批', color: 'volcano'},
  }[d.omit];
  }}
  <span class="layui-badge febs-tag-{{omit.color}}">{{ omit.title }}</span>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        $view = $('#febs-breakageStat'),
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        sortObject = {field: 'month', type: 'asc'},
        tableIns;

    form.render();

    initTable();

    laydate.render({
      elem: '#breakageStat-month',
      range: true,
      trigger: 'click'
    });

    table.on('sort(breakageStatTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
        initSort: obj,
        where: $.extend(getQueryParams(), {
          field: obj.field,
          order: obj.type
        })
      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    function initTable() {
      tableIns = febs.table.init({
        elem: $view.find('table'),
        id: 'breakageStat',
        url: ctx + 'breakageStat/list',
        cols: [[
          {type: 'checkbox'},
          {title: '书名', field: 'bookname', edit: 'text'},
          {title: '出版社', field: 'pubid'},
          {field: 'bookmoney', title: '价格'},
          {field: 'booktype', title: '图书类型'},
          {field: 'number', title: '报损数量'},
          {title: '损坏类型', templet: '#breakageStat-bookfrom'},
          {title: '损失总额',  field: 'totalmoney'},
          {title: '年份', field: 'year'},
          {title: '月份', field: 'month', sort: 'true'},
          {title: '状态', templet: '#breakageStat-omit'},
        ]]
      });
    }

    function getQueryParams() {
      return {
        bookname: $searchForm.find('input[name="bookname"]').val().trim(),
        booktype: $searchForm.find('input[name="booktype"]').val(),
        bookfrom: $searchForm.find('select[name="bookfrom"]').val().trim(),
        month: $searchForm.find('input[name="month"]').val()
      };
    }
  })
</script>